﻿#include "newfinancewidget.h"
#include "ui_newfinancewidget.h"

NewFinanceWidget::NewFinanceWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewFinanceWidget)
{
    ui->setupUi(this);

    setWindowTitle("新建财务记录");

    connect(&financeService, &FinanceService::addFinanceRecordSuccess,
            [=]()
    {
        this->close();
    });
}

NewFinanceWidget::~NewFinanceWidget()
{
    delete ui;
}

void NewFinanceWidget::showWithFinanceItem(FinanceItem *financeItem){
    this->financeItem = financeItem;

    ui->labelType->setText(financeItem->type==0?"收/支：收入":"收/支：支出");
    ui->labelCategory->setText("财务类别："+financeItem->category);
    ui->labelSubject->setText("财务科目："+financeItem->subject);
    ui->lineEditMoney->clear();
    ui->lineEditRemark->clear();

    exec();
}

void NewFinanceWidget::on_pushButtonCancel_clicked()
{
    this->close();
}

void NewFinanceWidget::on_pushButtonOK_clicked()
{
    if(userService.inputOperatePassword(this) == false){
        return;
    }
    double money = ui->lineEditMoney->text().toDouble();
    QString remark = ui->lineEditRemark->text();

    financeService.addFinanceRecord(this, ui->pushButtonOK, financeItem, money, remark);
}

void NewFinanceWidget::on_lineEditMoney_editingFinished()
{
    double money = ui->lineEditMoney->text().toDouble();
    if(money < 0){
        money = 0;
    }

    ui->lineEditMoney->setText(QString("%1").arg(money));
}
